#!/bin/sh
# Copyright (c) 2010, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# This script runs a collection of unit-tests and reports their
# status to stdout according to the Test Anything Protocol (TAP):
#   http://testanything.org/wiki/index.php/Main_Page
#   http://en.wikipedia.org/wiki/Test_Anything_Protocol
# Any additional, non-TAP output by this script and the unit tests is
# redirected to a local log file.

# TAP: report test plan
plan="1..8"
echo "$plan"

this_dir="`dirname $0`"
this_name="`basename $0`"

# log file for non-TAP output from this script
log="$this_dir/$this_name.log"
echo "# detailed ndbjtie test output in log file: '$log'"
rm -f "$log"
touch "$log"
echo "running tests: $plan" >> "$log" 2>&1

test_counter=0

# Runs a unit test script and report its status as TAP.
# Arguments: unit test hame
run_test()
{
    test_name=$1;
    test_counter=`expr $test_counter + 1`
    script_dir="$this_dir"
    script_name="test_$test_name.sh"
    script_path="$script_dir/$script_name"

    echo "\nrunning test '$script_path':" >> "$log" 2>&1
    if [ ! -x "$script_path" ]; then
       status="not ok $test_counter - $test_name # missing file '$script_path'"
    else
      s=""
      ( cd $script_dir ; ./$script_name ) >> "$log" 2>&1
      if [ "$?" -ne "0" ]; then
        s="not "
      fi
      status="${s}ok $test_counter - ndbjtie test: $test_name"
    fi;
    echo "$status" >> "$log" 2>&1

    # TAP: report unit test status
    echo "$status"
}

run_test "mutils"
run_test "ndbjtie_constants"
run_test "ndbjtie_multilib"
run_test "ndbjtie_smoke"
run_test "unload_mutils"
run_test "unload_ndbjtie_constants"
run_test "unload_ndbjtie_multilib"
run_test "unload_ndbjtie_smoke"

echo "\ndone." >> "$log" 2>&1
